/**
 * Chrowety
 * bg/plugins.short.js
 */

var 
plugins = window.plugins || {};

(function( w ){

	function short( options, callback )
	{
		return new short.fn.init( options, callback );
	}
	
	short.fn = {
			init: function( options, callback )
			{							
				if( isObj( options ) )
				{
					if( isString( options.url ) )
						this.url = options.url;
					else
						return console.error('plugins.shortURL: url not defined', options);
					
					if( isString( options.service ) && this.api[ options.service ] )
						this.service = options.service;
					else{
						console.warn('plugins.shortURL: The service received does not exists.', options.service);
						this.service = cfg.get('urlShortener.service') || 'goo.gl';
					}						
				}
				else if( isString( options ))
				{
					this.url = options;
					this.service = cfg.get('urlShortener.service') || 'goo.gl';
				}
				else					
					return console.error('plugins.shortURL: url not defined', options);
				
												
				this.dados = {};
				this.method = 'GET';
				this.type = 'text';
				this.reTurn = null;								
				
				switch( this.service )
				{
					case 'goo.gl':
						this.dados['url'] = this.url;
						this.method = 'POST';
						this.type = 'json';
						this.reTurn = 'short_url';
						break;
						
                    case 'goo.gl/maps':
						this.dados['q'] = this.url;
						this.dados['abauth'] = this.gMapsAuth;
						this.type = 'json';
						this.reTurn = 'short_url';
						break;

					case 'bit.ly':
					case 'j.mp':
						this.dados['longUrl'] = this.url;
						this.dados['apiKey'] = cfg.get('urlShortener.bitlyKey');
						this.dados['login'] = cfg.get('urlShortener.bitlyUser');
						this.dados['format'] = 'txt';
						break;
						
					case 'is.gd':
						this.dados['longurl'] = this.url;
						break;

					case 'miud.in':					
					case 'migre.me':
						this.dados['url'] = this.url;
						break;
						
					case '3.ly':
						this.dados['u'] = encodeURIComponent( this.url );
						this.dados['api'] = 'pj7382946378';
						break;												
				}
				
				var _this = this;
				
				this.request(function( ok, result, status ){
					
					if( isFunction(callback) )
					{
						if( ok )
						{
							callback(
									_this.reTurn ? 
											result[_this.reTurn] : 
												result
							);
						}
						else
						{
							callback('');
							
							if( 500 === status )
								_this.gMapsAuth = null;
						}
					}
				});				
			},
			
			request: function( callback )
			{
				var 
				url = this.api[ this.service ],
				req = new Request( url, this.method, this.type ? this.type : 'json' );
				req.dados = this.dados || {};
				req.sucesso = function( r, status, xhr )
				{
					if( isFunction(callback) ) 
						callback( true, r, status );
				};
				req.erro = function( xhr, status, error )
				{
					if( isFunction(callback) ) 
						callback( false, error, status );
				};
				req.init({noOAuth:true});
			},
			
			api: {
				'miud.in':		'http://miud.in/api-create.php',
				'migre.me': 	'http://migre.me/api.txt',
				'3.ly':			'http://3.ly',
				'goo.gl':		'http://goo.gl/api/url',
				'goo.gl/maps':  'http://maps.google.com/maps/urlshortener',
				'bit.ly':		'http://api.bit.ly/v3/shorten',
				'j.mp':			'http://api.j.mp/v3/shorten',
				'is.gd':		'http://is.gd/api.php'
			}
	};
	
	short.fn.init.prototype = short.fn;
	
	w.plugins.shortURL = short;
	
})( this );